#!/usr/bin/perl -w
# configure-mediaproxy.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
use strict; use warnings;

package CCNProxy::Provision::Application::mediaproxy;
use base qw(CCNProxy::Provision::Base);
use CCNCore::Scripting;
use CCNCore::Home;

# Converted to pk scheme.

sub run()
{
    my $self = shift;
    return if $self->p->force_manager;

    $self->CHECKFOR('/usr/src/mediaproxy/proxydispatcher.py');
    $self->CHECKFOR('/usr/src/mediaproxy/mediaproxy.py');
    $self->CHECKFOR('/usr/src/mediaproxy/modules');

    if( $self->p->operating_system =~ /^(debian|ccn)/ )
    {
        $self->CHECKFOR('/usr/src/mediaproxy/boot/mediaproxy.debian');
    }
    else
    {
        $self->CHECKFOR('/usr/src/mediaproxy/boot/mediaproxy.other');
    }

    # Install the programs as needed
    our $install_dir = '/usr/local/mediaproxy/';
    if($self->upgrading)
    {
        CCNCore::Scripting::exec_command(CCNCore::Home::manager."/lib/CCNProxy/bin/upgrade/mediaproxy.sh $install_dir ".$self->p->operating_system);
    }

    # Should mediaproxy be enabled?
    return $self->disable('mediaproxy') 
        unless defined $self->p->{px_member} or defined $self->p->{mp_member};

    $self->enable('mediaproxy');

    # Configuration file for proxydispatcher.py and mediaproxy.py

    my $mp_cluster = $self->sql_get( 
        'SELECT mp_cluster FROM cluster WHERE pk = ?', $self->p->{px_member} );

    my $start_dispatcher = (defined $self->p->{px_member} && defined $mp_cluster) ? 'yes' : 'no';
    my $start_media      = (defined $self->p->{mp_member}) ? 'yes' : 'no';

    my $defaultProxy = '';
    $defaultProxy = qq(defaultProxy = domain://).$self->p->make_cluster_name($mp_cluster,'mp').qq(\n)
        if defined $mp_cluster;
    
    my @allow_ips = ();
    if( defined $self->p->{mp_member} )
    {
        # Select the servers in the proxy clusters that may use
        # this mediaproxy cluster.
        $self->sql_do_for(
            'SELECT server.pk FROM server, cluster
                WHERE server.px_member = cluster.pk AND cluster.mp_cluster = ?', $self->p->{mp_member},
        sub
        {
            push @allow_ips, $self$self->p->make_server_name(shift);
        });
    }
    
    my $allow_ips = join(', ',@allow_ips);

    my $signaling_ip = $self->get_replication_ip_of($self->p->server_pk);
    my $bearer_ip = $self->get_realtime_ip_of($self->p->server_pk);
    
    foreach( '/usr/local/mediaproxy/mediaproxy.ini' )
    {
        CAT $_, <<"EOT";
[Dispatcher]
start = $start_dispatcher
socket = /var/run/mediaproxy.sock
; group = ser
$defaultProxy

[MediaProxy]
start = $start_media
socket = None
; group = ser
listen = $signaling_ip
allow = $allow_ips
proxyIP = $bearer_ip
portRange = 49152:65534
TOS = 0xb8
idleTimeout = 60
holdTimeout = 3600
forceClose = 0

[Accounting]
start = no
dbaccounting = off

;[Database]
;user = 
;password = 
;host = 
;database = 
;table = 

EOT
        IF_CHANGED $_ => '/etc/init.d/mediaproxy restart';
    }
}

1;